package cn.bsb.controller.backstage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import cn.bsb.pojo.Article;
import cn.bsb.pojo.DevUser;
import cn.bsb.pojo.Thematic;
import cn.bsb.service.backstage.ArticleService;
import cn.bsb.service.backstage.DevUserService;
import cn.bsb.service.backstage.ThematicService;

@Controller
@RequestMapping(value = "article")
public class articleController {
	@Autowired
	private ArticleService article;
	@Autowired
	private DevUserService devUser;
	@Autowired
	private ThematicService thematic;
	
	//文章列表
	@RequestMapping("inList")
	public String inList(Model model,
			HttpSession session,HttpServletRequest request,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		//文章分页
		PageInfo<Article> page = article.getPage(pageNum, 10);
		model.addAttribute("page", page);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/articleList";
	}
	
	//搜索后文章列表
	@RequestMapping("inSelect")
	public String inSelect(@RequestParam("title") String title,
							Model model) {
		//文章列表
		List<Article> listarticle = article.getList(title);
		model.addAttribute("listarticle", listarticle);
		//专题列表
		List<Thematic> thematicList = thematic.getThematicList();
		model.addAttribute("thematicList", thematicList);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/articleList";
	}
	//具体文章信息
	@RequestMapping("show")
	public String showArticle(@RequestParam("id") Long id,
			Model model, HttpSession session, HttpServletRequest request) {
		Article show = article.getOne(id);
		model.addAttribute("article", show);
		//用户列表
		List<DevUser> devUserList = devUser.getList();
		model.addAttribute("devUserList", devUserList);
		return "backstage/articleShow";
	}
	
	//删除文章
	@RequestMapping("delete")
	public String deleteOne(@RequestParam("id")Long id) {
		int a = article.delete(id);
		return "redirect:/article/inList";
	}

}
